package com.sunshine.controller;

import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.sunshine.common.entity.Result;
import com.sunshine.controller.convert.SubjectCategroyConvertDTO;
import com.sunshine.controller.dto.SubjectCategroyDTO;
import com.sunshine.domain.entity.SubjectCategroyBO;
import com.sunshine.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @description: 题目分类控制层
 * @Author 夏百友
 * @Date 2025/11/2 16:25
 */

@Slf4j
@RestController
@RequestMapping("/category")
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;


    /**
     * 添加题目分类
     *
     * @author 夏百友
     * @date 2025/11/2 18:23
     */
    @PostMapping("/add")
    public Result<Boolean> addSubjectCategory(@RequestBody SubjectCategroyDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectCategoryController.addSubjectCategory:参数{}", subjectCategoryDTO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父级分类不能为空");

            SubjectCategroyBO subjectCategoryBO = SubjectCategroyConvertDTO.INSTANCE.convertCategoryDtoToBo(subjectCategoryDTO);

            subjectCategoryDomainService.addSubjectCategory(subjectCategoryBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("controller.SubjectCategoryController.addSubjectCategory:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 查询全部分类
     *
     * @author 夏百友
     * @date 2025/11/5 13:31
     */

    @PostMapping("/queryAllCategory")
    public Result<List<SubjectCategroyDTO>> queryAllCategory() {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectCategoryController.queryAllCategory:无参数");
            }

            SubjectCategroyBO subjectCategroyBO = new SubjectCategroyBO();
            subjectCategroyBO.setParentId(0);
            List<SubjectCategroyBO> subjectCategoryBOList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategroyBO);

            log.info("controller.SubjectCategoryController.queryAllCategory:结果{}", subjectCategoryBOList);
            return Result.ok(SubjectCategroyConvertDTO.INSTANCE.convertCategoryBoToDtoList(subjectCategoryBOList));

        } catch (Exception e) {
            log.error("controller.SubjectCategoryController.queryAllCategory:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }


    /**
     * 查询分类大类
     *
     * @author 夏百友
     * @date 2025/11/4 18:20
     */

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategroyDTO>> queryPrimaryCategory(@RequestBody SubjectCategroyDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectCategoryController.queryPrimaryCategory:参数{}", subjectCategoryDTO);
            }

            SubjectCategroyBO subjectCategoryBO = SubjectCategroyConvertDTO.INSTANCE.convertCategoryDtoToBo(subjectCategoryDTO);

            List<SubjectCategroyBO> subjectCategoryBOList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBO);

            log.info("controller.SubjectCategoryController.queryPrimaryCategory:结果{}", subjectCategoryBOList);
            return Result.ok(SubjectCategroyConvertDTO.INSTANCE.convertCategoryBoToDtoList(subjectCategoryBOList));

        } catch (Exception e) {

            log.error("controller.SubjectCategoryController.queryPrimaryCategory:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 更新分类
     * @author 夏百友
     * @date 2025/11/5 13:54
     */
    @PostMapping("/updateCategory")
    public Result<Boolean> update(@RequestBody SubjectCategroyDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectCategoryController.update:参数{}", subjectCategoryDTO);
            }

            SubjectCategroyBO subjectCategoryBO = SubjectCategroyConvertDTO.INSTANCE.convertCategoryDtoToBo(subjectCategoryDTO);

            subjectCategoryDomainService.update(subjectCategoryBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("controller.SubjectCategoryController.update:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }


    /**
     * 删除分类
     * @author 夏百友
     * @date 2025/11/5 14:02
     */
    @GetMapping("/deleteCategory")
    public Result<Boolean> deleteCategory(@RequestBody SubjectCategroyDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectCategoryController.deleteCategory:参数{}", subjectCategoryDTO);
            }

            SubjectCategroyBO subjectCategoryBO = SubjectCategroyConvertDTO.INSTANCE.convertCategoryDtoToBo(subjectCategoryDTO);

            subjectCategoryDomainService.deleteCategory(subjectCategoryBO);

            Result<Boolean> result = Result.ok(true);
            result.setMessage("删除成功");
            return result;
        } catch (Exception e) {
            log.error("controller.SubjectCategoryController.deleteCategory:异常 {}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }



}